package com.moxiao.yfjh.module.blind.controller.admin.commonphrases;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.common.GenNoCommonMethod;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.commonphrases.CommonPhrasesConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.commonphrases.CommonPhrasesDO;
import com.moxiao.yfjh.module.blind.service.commonphrases.CommonPhrasesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲会员常用语")
@RestController
@RequestMapping("/blind/common-phrases")
@Validated
public class CommonPhrasesController {

    @Resource
    private CommonPhrasesService commonPhrasesService;
    @Resource
    private GenNoCommonMethod genNoCommonMethod;

    @PostMapping("/create")
    @Operation(summary = "创建相亲会员常用语")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:create')")
    public CommonResult<Long> createCommonPhrases(@Valid @RequestBody CommonPhrasesCreateReqVO createReqVO) {
        genNoCommonMethod.addCommonPhrasesNo(createReqVO);
        return success(commonPhrasesService.createCommonPhrases(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新相亲会员常用语")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:update')")
    public CommonResult<Boolean> updateCommonPhrases(@Valid @RequestBody CommonPhrasesUpdateReqVO updateReqVO) {
        commonPhrasesService.updateCommonPhrases(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲会员常用语")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:delete')")
    public CommonResult<Boolean> deleteCommonPhrases(@RequestParam("id") Long id) {
        commonPhrasesService.deleteCommonPhrases(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲会员常用语")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:query')")
    public CommonResult<CommonPhrasesRespVO> getCommonPhrases(@RequestParam("id") Long id) {
        CommonPhrasesDO commonPhrases = commonPhrasesService.getCommonPhrases(id);
        return success(CommonPhrasesConvert.INSTANCE.convert(commonPhrases));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲会员常用语列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:query')")
    public CommonResult<List<CommonPhrasesRespVO>> getCommonPhrasesList(@RequestParam("ids") Collection<Long> ids) {
        List<CommonPhrasesDO> list = commonPhrasesService.getCommonPhrasesList(ids);
        return success(CommonPhrasesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲会员常用语分页")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:query')")
    public CommonResult<PageResult<CommonPhrasesRespVO>> getCommonPhrasesPage(@Valid CommonPhrasesPageReqVO pageVO) {
        PageResult<CommonPhrasesDO> pageResult = commonPhrasesService.getCommonPhrasesPage(pageVO);
        return success(CommonPhrasesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲会员常用语 Excel")
    @PreAuthorize("@ss.hasPermission('blind:common-phrases:export')")
    @OperateLog(type = EXPORT)
    public void exportCommonPhrasesExcel(@Valid CommonPhrasesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CommonPhrasesDO> list = commonPhrasesService.getCommonPhrasesList(exportReqVO);
        // 导出 Excel
        List<CommonPhrasesExcelVO> datas = CommonPhrasesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲会员常用语.xls", "数据", CommonPhrasesExcelVO.class, datas);
    }

}
